package com.hdax.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hdax.common.to.es.SkuEsModel;
import com.hdax.gulimall.product.entity.PmsSpuImagesEntity;
import com.hdax.gulimall.product.entity.PmsSpuInfoDescEntity;
import com.hdax.gulimall.product.entity.SkuImagesEntity;
import com.hdax.gulimall.product.service.*;
import com.hdax.gulimall.product.vo.ItemSaleAttrsVo;
import com.hdax.gulimall.product.vo.SkuItemVo;
import com.hdax.gulimall.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.common.utils.PageUtils;
import com.hdax.common.utils.Query;

import com.hdax.gulimall.product.dao.SkuInfoDao;
import com.hdax.gulimall.product.entity.SkuInfoEntity;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {


    @Autowired
    private SkuImagesService skuImagesService;

    @Autowired
    private PmsSpuInfoDescService pmsSpuInfoDescService;

    @Autowired
    private PmsAttrGroupService pmsAttrGroupService;

    @Autowired
    private PmsSkuSaleAttrValueService pmsSkuSaleAttrValueService;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfo) {
        int insert = this.baseMapper.insert(skuInfo);
    }


    /**
     * 查询商品的sku
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPageByCondtion(Map<String, Object> params) {
        /**
         * key
         * catelogId
         * brandId
         * min
         * max
         */
        QueryWrapper<SkuInfoEntity> skuInfoEntityQueryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            skuInfoEntityQueryWrapper.and(item->{
               skuInfoEntityQueryWrapper.eq("sku_id",key).or().like("sku_name",key);
            });
        }
        String catelogId = (String) params.get("catelogId");
        if(!StringUtils.isEmpty(catelogId)&&!"0".equalsIgnoreCase(catelogId)){
            skuInfoEntityQueryWrapper.eq("catalog_id",catelogId);
        }
        String brandId = (String) params.get("brandId");
        if(!StringUtils.isEmpty(catelogId)&&!"0".equalsIgnoreCase(brandId)){
            skuInfoEntityQueryWrapper.eq("brand_id",brandId);
        }
        String min = (String) params.get("min");
        if(!StringUtils.isEmpty(catelogId)){
            skuInfoEntityQueryWrapper.eq("price",min);
        }
        String max = (String) params.get("max");
        if(!StringUtils.isEmpty(catelogId)&&!"0".equalsIgnoreCase(max)){
            try {
                BigDecimal bigDecimal=new BigDecimal(min);
                if(bigDecimal.compareTo(new BigDecimal("0"))==1){
                    skuInfoEntityQueryWrapper.le("price",max);
                }
            }catch (Exception e){

            }
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                skuInfoEntityQueryWrapper
        );
        return new PageUtils(page);
    }

    /**
     * 根据spuid查询所有的sku
     * @param
     * @return
     */
    @Override
    public List<SkuInfoEntity> getSkuBySpuId(Long spuinfo) {
        List<SkuInfoEntity> spu_id = this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuinfo));
        return spu_id;
    }


    /**
     * 根据商品的id，查询商品的详情，
     * @param skuId
     * @return
     */
    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo=new SkuItemVo();
        /**
         * 1、sku的基本信息  pms_sku_info
         */
        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfo = getById(skuId);
            skuItemVo.setInfo(skuInfo);
            return skuInfo;
        }, threadPoolExecutor);


        /**
         * 2、spu的销售属性
         */
        CompletableFuture<Void> voidCompletableFuture = infoFuture.thenAcceptAsync((res) -> {
            List<ItemSaleAttrsVo> list2 = pmsSkuSaleAttrValueService.getSaleAttrsBySpuId(res.getSpuId());
            skuItemVo.setSaleAttrs(list2);
        }, threadPoolExecutor);


        CompletableFuture<Void> voidCompletableFuture1 = infoFuture.thenAcceptAsync(res -> {
            List<SpuItemAttrGroupVo> spuItemAttrGroupVos = pmsAttrGroupService.getAttrGroupAttrsBySpuId(res.getSpuId(), res.getCatalogId());
            skuItemVo.setGroupAttrs(spuItemAttrGroupVos);
        }, threadPoolExecutor);

        /**
         * 3、获取spu的介绍
         */
        CompletableFuture<Void> spu_id1 = infoFuture.thenAcceptAsync(res -> {
            PmsSpuInfoDescEntity spu_id = pmsSpuInfoDescService.getOne(
                    new LambdaQueryWrapper<PmsSpuInfoDescEntity>().eq(PmsSpuInfoDescEntity :: getSpuId, res.getSpuId())
            );
            skuItemVo.setDesp(spu_id);
        }, threadPoolExecutor);

        /**
         * 4、sku的图片信息  pms_sku_images
         */
        CompletableFuture<Void> imagesFuture = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> list = skuImagesService.getImagesBySkuId(skuId);
            skuItemVo.setImages(list);
        }, threadPoolExecutor);


        //等待所有任务都完成
        CompletableFuture.allOf(infoFuture,voidCompletableFuture,voidCompletableFuture1,spu_id1,imagesFuture).get();

        return skuItemVo;
    }


}